package moyangjia.retailers.storage.sys.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import moyangjia.retailers.storage.sys.dao.SysGroupDao;
import moyangjia.retailers.storage.sys.dao.SysUserGroupDao;
import moyangjia.retailers.storage.sys.domain.SysGroup;
import moyangjia.retailers.storage.sys.domain.SysRoloG;
import moyangjia.retailers.storage.sys.domain.SysUserGroup;
import moyangjia.retailers.storage.sys.domain.dto.GroupDto;
import moyangjia.retailers.storage.sys.service.IGroupService;
import moyangjia.retailers.storage.sys.service.IRoleGService;
import moyangjia.retailers.storage.sys.dao.SysDictDao;
import moyangjia.retailers.storage.sys.dao.SysGroupDao;
import moyangjia.retailers.storage.sys.dao.SysRoloGDao;
import moyangjia.retailers.storage.sys.dao.SysUserGroupDao;
import moyangjia.retailers.storage.sys.domain.dto.GroupDto;
import moyangjia.retailers.storage.sys.service.IDictService;
import moyangjia.retailers.storage.sys.service.IGroupService;
import moyangjia.retailers.storage.sys.service.IRoleGService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @description:
 * @author: honglei
 * @time: 2021/3/15 0015 14:23
 */
@Slf4j
@Service
public class GroupServiceImpl extends ServiceImpl<SysGroupDao,SysGroup> implements IGroupService {
    @Autowired
    IRoleGService roleGService;
    @Autowired
    SysUserGroupDao userGroupDao;



    @Override
    public Object getList(SysGroup group) {
        IPage<SysGroup> page=new Page<>();
        Wrapper<SysGroup> wrapper=new QueryWrapper<>(group);
        page=this.baseMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public Object getDetail(String id) {
        SysGroup group=getById(id);
        if (null==group){
            return group;
        }
        GroupDto groupDto=new GroupDto();
        BeanUtils.copyProperties(group,groupDto);

        SysRoloG sysRoloG=new SysRoloG();
        sysRoloG.setGid(group.getId());
        QueryWrapper<SysRoloG> wrapper=new QueryWrapper(sysRoloG);
        groupDto.setRoles(roleGService.list(wrapper));

        SysUserGroup userGroup=new SysUserGroup();
        userGroup.setGid(group.getId());
        Wrapper<SysUserGroup> queryWrapper=new QueryWrapper(userGroup);
        groupDto.setUsers(userGroupDao.selectList(queryWrapper));

        return groupDto;
    }

    @Transactional
    @Override
    public boolean updateExtend(GroupDto group) {
        if (null==group){
            return false;
        }
       /*先删后增如果存在角色更新绑定角色*/
        SysRoloG sysRoloG=new SysRoloG();
        sysRoloG.setGid(group.getId());
        QueryWrapper<SysRoloG> wrapper=new QueryWrapper(sysRoloG);

        roleGService.remove(wrapper);
        if (!CollectionUtils.isEmpty(group.getRoles())){
            for (SysRoloG sysRole : group.getRoles()) {
               sysRole.setGid(group.getId());
            }
            roleGService.saveBatch(group.getRoles());
        }

        /* 先删后增 如果存在角色更新绑定角色*/
        SysUserGroup userGroup=new SysUserGroup();
        userGroup.setGid(group.getId());
        QueryWrapper<SysUserGroup> userW=new QueryWrapper(userGroup);
        userGroupDao.delete(userW);
        if (!CollectionUtils.isEmpty(group.getUsers())){
            for (SysUserGroup u : group.getUsers()) {
                u.setGid(group.getId());
            }
            addUserGroups(group.getUsers());
        }
        return true;
    }

    @Override
    public int addUserGroup(SysUserGroup userGrou) {
        return userGroupDao.insert(userGrou);
    }

    @Transactional
    @Override
    public int addUserGroups(List<SysUserGroup> userGroups) {
        int count=0;
        for (SysUserGroup userGroup : userGroups) {
            count=count+userGroupDao.insert(userGroup);
        }
        return count;
    }

    @Override
    public int romeveUserGroup(Serializable id) {
        return userGroupDao.deleteById(id);
    }
}
